#!/usr/bin/env pwsh

# GinAdmin 部署脚本 (PowerShell版)
# 本脚本用于Windows环境下的项目部署

# 设置变量
$appName = "GinAdmin"
$appExe = "GinAdmin.exe"
$buildPath = ".\build"
$configPath = ".\configs"
$logPath = ".\logs"
$scriptPath = ".\scripts"
$serviceName = "GinAdminService"

# 显示部署信息
Write-Host "
-----------------------------------------
      GinAdmin 系统部署工具 (Windows)
-----------------------------------------
"

# 检查是否有管理员权限
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $isAdmin) {
    Write-Host "错误: 请使用管理员权限运行此脚本" -ForegroundColor Red
    exit 1
}

# 创建构建目录
if (-not (Test-Path -Path $buildPath)) {
    New-Item -ItemType Directory -Path $buildPath | Out-Null
    Write-Host "创建构建目录: $buildPath" -ForegroundColor Green
}

# 编译程序
Write-Host "正在编译程序..." -ForegroundColor Yellow
try {
    & go build -o "$buildPath\$appExe" .\cmd\main.go
    if ($LASTEXITCODE -ne 0) {
        throw "编译失败，请检查错误信息"
    }
    Write-Host "编译成功: $buildPath\$appExe" -ForegroundColor Green
}
catch {
    Write-Host "编译失败: $_" -ForegroundColor Red
    exit 1
}

# 复制配置文件
Write-Host "正在复制配置文件..." -ForegroundColor Yellow
if (Test-Path -Path "$configPath\config.yaml") {
    Copy-Item -Path "$configPath\config.yaml" -Destination "$buildPath\config.yaml" -Force
    Write-Host "配置文件已复制" -ForegroundColor Green
}
else {
    Write-Host "警告: 配置文件不存在 ($configPath\config.yaml)" -ForegroundColor Yellow
}

# 复制启动脚本
Write-Host "正在复制启动脚本..." -ForegroundColor Yellow
if (Test-Path -Path "$scriptPath\start.bat") {
    Copy-Item -Path "$scriptPath\start.bat" -Destination "$buildPath\start.bat" -Force
    Write-Host "启动脚本已复制" -ForegroundColor Green
}
else {
    Write-Host "警告: 启动脚本不存在 ($scriptPath\start.bat)" -ForegroundColor Yellow
}

# 创建日志目录
if (-not (Test-Path -Path "$buildPath\logs")) {
    New-Item -ItemType Directory -Path "$buildPath\logs" | Out-Null
    Write-Host "创建日志目录: $buildPath\logs" -ForegroundColor Green
}

# 检查是否安装为Windows服务
Write-Host "是否将应用安装为Windows服务? (需要已安装NSSM工具)" -ForegroundColor Yellow
$installService = Read-Host "(y/n)"

if ($installService -eq 'y' -or $installService -eq 'Y') {
    # 检查NSSM是否安装
    $nssmPath = (Get-Command "nssm.exe" -ErrorAction SilentlyContinue).Source
    
    if (-not $nssmPath) {
        Write-Host "错误: 未找到NSSM工具，请先安装NSSM (https://nssm.cc/)" -ForegroundColor Red
        exit 1
    }
    
    # 检查服务是否已存在
    $serviceExists = Get-Service -Name $serviceName -ErrorAction SilentlyContinue
    
    if ($serviceExists) {
        Write-Host "服务已存在，正在停止并移除..." -ForegroundColor Yellow
        & nssm.exe stop $serviceName
        & nssm.exe remove $serviceName confirm
    }
    
    # 安装服务
    Write-Host "正在安装Windows服务: $serviceName" -ForegroundColor Yellow
    $exePath = Resolve-Path "$buildPath\$appExe"
    & nssm.exe install $serviceName $exePath
    & nssm.exe set $serviceName DisplayName "$appName Service"
    & nssm.exe set $serviceName Description "GinAdmin管理系统服务"
    & nssm.exe set $serviceName AppDirectory $buildPath
    & nssm.exe set $serviceName AppStdout "$buildPath\logs\service.log"
    & nssm.exe set $serviceName AppStderr "$buildPath\logs\service_error.log"
    
    # 启动服务
    & nssm.exe start $serviceName
    
    if (Get-Service -Name $serviceName -ErrorAction SilentlyContinue | Where-Object { $_.Status -eq "Running" }) {
        Write-Host "服务安装并启动成功: $serviceName" -ForegroundColor Green
    }
    else {
        Write-Host "警告: 服务安装成功但启动失败，请手动检查服务状态" -ForegroundColor Yellow
    }
}

# 部署完成
Write-Host "
-----------------------------------------
      GinAdmin 系统部署完成
-----------------------------------------
部署路径: $buildPath
可执行文件: $appExe
配置文件: config.yaml
" -ForegroundColor Cyan

if ($installService -eq 'y' -or $installService -eq 'Y') {
    Write-Host "Windows服务名称: $serviceName" -ForegroundColor Cyan
}
else {
    Write-Host "启动方式: 执行 $buildPath\start.bat" -ForegroundColor Cyan
}

Write-Host "-----------------------------------------" 